<template>
  <a-descriptions size="small" :column="1" bordered>
    <a-descriptions-item :label="$t('383')">
      {{ appStore.aryVgt.find((vgt) => vgt.key == props.vg.groupType.T).label }}
    </a-descriptions-item>
    <a-descriptions-item label="可写入数据">
      {{ props.vg.writeAble }}
    </a-descriptions-item>
    <a-descriptions-item :label="$t('384')">
      {{ Utils.describeShareRuleModel(props.vg.ruleM) }}
    </a-descriptions-item>
    <a-descriptions-item :label="$t('385')">
      <a-list
        v-if="CommonDefine.usefulArray(props.vg.ruleF)"
        item-layout="horizontal"
        :data-source="lstSrF"
      >
        <template #renderItem="{ item }">
          <a-list-item>{{ item }}</a-list-item>
        </template>
      </a-list>
    </a-descriptions-item>
    <a-descriptions-item label="值访问限制">
      <a-list
        v-if="CommonDefine.usefulArray(props.vg.ruleFv)"
        item-layout="horizontal"
        :data-source="lstSrFv"
      >
        <template #renderItem="{ item }">
          <a-list-item>{{ item }}</a-list-item>
        </template>
      </a-list>
    </a-descriptions-item>
    <a-descriptions-item :label="$t('386')" v-if="props.showUser">
      <a-list-item>{{ Utils.getUserName(props.vg.uId) }}</a-list-item>
    </a-descriptions-item>
  </a-descriptions>
</template>
<script setup>
import { watch } from "vue";
import CommonDefine from "@/component/CommonDefine";
import Utils from "@/component/Utils";
import { reactive } from "vue";

const appStore = CommonDefine.appStore();
const props = defineProps({
  vg: Object,
  showUser: { type: Boolean, default: false },
});
const lstSrF = reactive([]);
const lstSrFv = reactive([]);

const describeShareLimit = (limit) => {
  let fn = (fields) => {
    let r = appStore.aryVisitLimitType.find((x) => x.key == limit.T).label;
    r += " 参与字段";
    limit.C.forEach((fid) => {
      r += " " + fields.find((x) => x.id == fid).label;
    });
    r += "; ";
    lstSrF.push(r);
  };
  CommonDefine.runWithFieldCache(props.vg.modelId, fn);
};

const describeFieldValueEvaluation = (fve) => {
  let fn = (fields) => {
    let r = fields.find((x) => x.id == fve.C[0]).label;
    r += " ";
    r += appStore.aryFieldValueEvaluation.find((x) => x.key == fve.T).label;
    r += " ";
    r += fve.C[1];
    r += "; ";
    lstSrFv.push(r);
  };
  CommonDefine.runWithFieldCache(props.vg.modelId, fn);
};

watch(
  () => props.vg,
  (newValue) => {
    CommonDefine.clearArray(lstSrF);
    newValue.ruleF.forEach((rule) => {
      describeShareLimit(rule);
    });
    CommonDefine.clearArray(lstSrFv);
    newValue.ruleFv.forEach((rule) => {
      describeFieldValueEvaluation(rule);
    });
  },
  {
    immediate: true,
  }
);
</script>
